/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-03
*/
package com.rzico.basics.controller.admin;

import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Designer;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.WxPage;
import com.rzico.basics.service.DesignerService;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.WxPageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设计器控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-03
 */
@Api(description = "设计器接口")
@RestController
@RequestMapping("/admin/designer")
public class DesignerAdminController extends BaseController {

    @Autowired
    private DesignerService designerService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private WxPageService wxPageService;

    /**
     * 查询组件列表
     *
     * @return
    */
    @ApiOperation("查询组件列表")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageId", value = "页面Id", dataType = "Integer", allowMultiple = true, paramType = "query")})
    public CommResult<Designer> list(Long pageId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sortField", "orders");
        params.put("sortType", "ASC");

        Enterprise enterprise = enterpriseService.getCurrent();
        params.put("enterpriseId",enterprise.getId());

        if (pageId==null) {
            WxPage wxPage = wxPageService.getHome(enterprise);
            if (wxPage==null) {
                //兼容历史
                pageId = 0L;
            } else {
                pageId = wxPage.getId();
            }
        }
        params.put("pageId",pageId);

        List<Designer> list = designerService.selectList(params);
        return CommResult.success(list);
    }

    /**
     * 更新设计器
     *
     * @param designers
     * @return
     */
    @Log(desc = "更新设计器", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新设计器")
    @PostMapping("/update")
    public CommResult<Designer> save(@RequestBody List<Designer> designers) {

        if (designers.size()==0) {
            return CommResult.error("请添加组件");
        }
        Enterprise enterprise = enterpriseService.getCurrent();

        WxPage wxPage = wxPageService.getHome(enterprise);
        //兼容历史
        for (Designer designer:designers) {
            if (wxPage!=null && designer.getPageId()==null) {
                designer.setPageId(wxPage.getId());
            }
        }

        int affectCount = designerService.save(designers);
        if (affectCount <= 0){
            return CommResult.error();
        }
        return CommResult.success("更新成功");
    }

}
